package com.zb.emq;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.zb.entity.TbQuotaModel;
import com.zb.service.TbQuotaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Random;

@Component
public class Mock {

    @Autowired
    private TbQuotaService quotaService;

    @Autowired
    private EmqClient emqClient;


    /*
    {
        "sn":"1001",
        "temp":"51",
        "humidity":"20",
    }
    {
        "sn":"1001",


    }
     */
//    @Scheduled(cron = "0/10 * * * * ?")
    public void addData() {
        System.out.println(" 定时任务。。。。");
        List<TbQuotaModel> quotaList = quotaService.list();
        for (int i = 0; i < 10; i++) {//外层循环设备
            String device="100"+i;
            Map<String,Object> map = Maps.newHashMap();
            String subject ="";
            for (TbQuotaModel quotaModel : quotaList) { //内存循环指标，因为一个设备有多个指标
                map.put(quotaModel.getSnKey(),device);
                Random random = new Random();
                int quotaValue = random.nextInt(100);
                map.put(quotaModel.getValueKey(),quotaValue);
                subject=quotaModel.getSubject();
            }
            String json = JSON.toJSONString(map);
            emqClient.publish(subject,json);
            try {
                System.out.println("------------------>"+json);
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }


}
